package org.example.ioc.pro4_ioc_factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Pro4_App_Test {
    public static void main(String[] args) throws Exception {
        ApplicationContext context = new AnnotationConfigApplicationContext(Pro4_App_Test.class);

        String[] beanNames = context.getBeanDefinitionNames();
        for (String s : beanNames) {
            System.out.println(s);
        }

        //通过工厂bean 名字得到的是   工厂生产的bean对象  而不是工厂 bean 本身
        Fruit fruit = (Fruit) context.getBean("fruitFactoryBean");
        fruit.getName();
        System.out.println(fruit);

        //如果真想取出工厂 bean 本身，需要在工厂 bean 的名字前面加上  &
        FruitFactoryBean fruitFactoryBean = (FruitFactoryBean) context.getBean("&fruitFactoryBean");
        fruitFactoryBean.getObject().getName();
        System.out.println(fruitFactoryBean);




    }

    @Bean
    public FruitFactoryBean fruitFactoryBean(){
        return new FruitFactoryBean();
    }

}
